//
//  CPLanguageModel.m
//  ColorPickerClient
//
//  Created by laomi on 2020/8/12.
//  Copyright © 2020 laomi. All rights reserved.
//

#import "CPLanguageModel.h"
#import "NSString+language.h"
#import "LAOMICommonChoiceProtocol.h"

LanguageIdentifier *const CP_English_Identifier = @"en"; //英文标识
LanguageIdentifier *const CP_Chinses_Identifier = @"zh-Hans"; // 中文标识
LanguageIdentifier *const CP_Auto_Identifier = @"system"; // 自动
NSString *const CP_Last_Selected_Language_Identifier = @"CP_Last_Selected_Language_Identifier_Key"; // 自动

@interface CPLanguageModel()
{

}
@property(nonatomic,strong)NSArray *privateAll;
@end

@implementation CPLanguageModel

+(instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    static CPLanguageModel *varInstance;
    dispatch_once(&onceToken, ^{
        varInstance = [[CPLanguageModel alloc]init];
    });
    return varInstance;
}
-(NSString*)name
{
    return [CPLanguageModel nameForLanguageIdentifier:self._id];
}

+(NSString*)nameForLanguageIdentifier:(LanguageIdentifier *)identifier
{
    if ([identifier isEqualToString:CP_English_Identifier]) {
        return @"En";
    }else if ([identifier isEqualToString: CP_Chinses_Identifier])
    {
        return @"简体中文";
    }else {
        return [@"system.language.auto" stringForLanguage:[NSString laomi_languageIdentifier]];
    }
}
+(NSArray<CPLanguageModel*>*)allLanguage;
{
    if ([CPLanguageModel shareInstance].privateAll) {
        return [CPLanguageModel shareInstance].privateAll;
    }else{
        NSArray *arr = @[CP_Auto_Identifier,CP_Chinses_Identifier,CP_English_Identifier];
        NSMutableArray *all = [NSMutableArray array];
        for (LanguageIdentifier *language in arr) {
            CPLanguageModel *model = [CPLanguageModel new];
            model._id = language;
            [all addObject:model];
        }
       [CPLanguageModel shareInstance].privateAll = all;
        return [CPLanguageModel shareInstance].privateAll;
    }
}
+(NSString*)lastLanguage
{
    
    return [[NSUserDefaults standardUserDefaults] objectForKey:CP_Last_Selected_Language_Identifier]?[[NSUserDefaults standardUserDefaults] objectForKey:CP_Last_Selected_Language_Identifier]:CP_Auto_Identifier;
}
@synthesize isSelected;

@end
